//
//  HomeSubVC.m
//  MyStock
//
//  Created by 刘翔 on 2024/10/22.
//

#import "HomeSubVC.h"
#import "HomeCell.h"
#import "GLDetailVC.h"

@interface HomeSubVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)NSArray *tips;
@property(nonatomic,strong)NSArray *tldStocks;
@property (strong, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIButton *strongBtn;

@property (weak, nonatomic) IBOutlet UIButton *zhuliBtn;
@property(nonatomic,strong)NSArray *hotgls;

@property(nonatomic,strong)NSMutableArray *sortHotgls;
@property(nonatomic,strong)NSMutableArray *list_son;

@property(nonatomic,strong)NSMutableArray *sonCodes;
@property(nonatomic,strong)NSArray *sonHotgls;

@property(nonatomic, strong) dispatch_source_t timer;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *states;
@property(nonatomic,strong) NSMutableArray *headerNavModels;
@property(nonatomic, strong) NSMutableArray<ScreenNavBarItem *> *itemArray;

@end

@implementation HomeSubVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.states = @[@(DataSortStateAscending), @(DataSortStateNomal)];
    self.sortHotgls = [NSMutableArray array];
    self.list_son = [NSMutableArray array];
    self.sonCodes = [NSMutableArray array];
    
    self.customNavBar.hidden = YES;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.mj_header = [LxResfreshHeader headerWithRefreshingBlock:^{
        [self loadHotGl];
      }];
    [self.tableView.mj_header beginRefreshing];
    [self timerRefreshData];
    [self setHeader];
       
}

- (void)setHeader{

    self.headerNavModels = [NSMutableArray array];
    CGFloat itemWidth = kScreenWidth / 3;
    NSArray *titles = @[@"强度",@"主力净额"];
    for (int i = 0; i < titles.count; i++) {
        SortHeaerModel *model = [[SortHeaerModel alloc] initWithTitle:titles[i] normalImage:@"" selectSingleImage:@"" selectDoubleImage:@""];
        model.type = ItemSelelctTypeNone;
        model.itemCode = i + 100;
        [self.headerNavModels addObject:model];
        
        ScreenNavBarItem *itemView = [[ScreenNavBarItem alloc] initWithItemModel:self.headerNavModels[i]];
        itemView.frame = CGRectMake(kScreenWidth - (titles.count - i) * itemWidth, 0, itemWidth, 40);
        itemView.tag = 100 + i;
        [self.headerView addSubview:itemView];
      
    }
}


- (void)timerRefreshData{
        
    //0.创建队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_time_t startTime = dispatch_time(DISPATCH_TIME_NOW, 0);
    dispatch_source_set_timer(self.timer, startTime, 5 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    //3.要调用的任务
    dispatch_source_set_event_handler(self.timer, ^{
        [self loadHotGl];

    });
    
    //4.开始执行
    dispatch_resume(self.timer);
        
}


- (void)loadHotGl{
    
    //时间段RStart=0925 REnd=1020
    BADataEntity *entity = [BADataEntity new];
    entity.urlString = [NSString stringWithFormat:@"%@?%@",KPUrl,[NSString stringWithFormat:@"Index=0&Order=1&PhoneOSNew=2&Type=1&VerSion=5.15.0.3&ZSType=7&a=RealRankingInfo&apiv=w37&c=ZhiShuRanking&st=30"]];
    entity.needCache = NO;
    entity.parameters = nil;
    //主力净额 6
    
    [BANetManager ba_request_GETWithEntity:entity successBlock:^(id response) {
        NSDictionary *result = response;
        [self.sortHotgls removeAllObjects];
        [self.list_son removeAllObjects];
        [self.sonCodes removeAllObjects];
        self.sonHotgls = result[@"list_soninfo"];
        
        NSArray *array = result[@"list_son"];
        for (int i = 0; i < array.count; i++) {
            [self.list_son addObject:[NSString stringWithFormat:@"%@",array[i]]];
        }
        
        if ([result[@"errcode"] intValue] == 0) {
            self.hotgls = result[@"list"];
            for (int i = 0; i < self.hotgls.count; i++) {
                [self.sortHotgls addObject:self.hotgls[i]];
                NSString *code = self.hotgls[i][0];
                NSUInteger index = [self.list_son indexOfObject:code];
                if (index != NSNotFound) {
                    [self.sortHotgls addObject:self.sonHotgls[index]];
                    [self.sonCodes addObject:self.sonHotgls[index][0]];
                }
            }
            
            //19
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];

        }else{
            [self.tableView.mj_header endRefreshing];

        }
        
    } failureBlock:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        
    } progressBlock:^(int64_t bytesProgress, int64_t totalBytesProgress) {
        
    }];
}


#pragma  mark --------UITableView Delegete----------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sortHotgls.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *identifire = @"HomeCellID";
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifire];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"HomeCell" owner:nil options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
    
    NSArray *hot = self.sortHotgls[indexPath.row];
    cell.nameL.text = hot[1];
    cell.codeL.text = [NSString stringWithFormat:@"%@",hot[0]];
    cell.strengthL.text = [NSString stringWithFormat:@"%@",hot[2]];
    
    long zhulije = [hot[6] longValue];
  
    cell.rateL.textColor = [DataHandle handleColorWithLong:zhulije];
    cell.rateL.text = [DataHandle handleUnits:zhulije];
    NSUInteger index = [self.sonCodes indexOfObject:[NSString stringWithFormat:@"%@",hot[0]]];
    if (index != NSNotFound) {
        cell.leftCon.constant = 40;
        cell.line1.hidden = cell.line2.hidden = NO;

    }else{
        cell.leftCon.constant = 20;
        cell.line1.hidden = cell.line2.hidden = YES;


    }
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    return 70;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   
    return 40;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    return self.headerView;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *hot = self.sortHotgls[indexPath.row];
    HotGLVC *vc = [[HotGLVC alloc] init];
    vc.glName = hot[1];
    
    [self.navigationController pushViewController:vc animated:YES];
        
}

#pragma mark - JXCategoryListContentViewDelegate
- (UIView *)listView {
    return self.view;
}

- (void)listDidAppear {
    
}

- (void)listDidDisappear {
    
}



@end
